<?php

namespace App\Http\Controllers\CmsApi;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Lib\Response;
use App\Http\Controllers\BaseController;
use App\Http\Model\Hint;
use App\Http\Services\RoomFrameServices;


class InnerRoomFrameController extends BaseController
{
    protected $roomFrameService;

    public function __construct()
    {
        $this->roomFrameService = new RoomFrameServices();

    }

    /**
     * 房间边框新增
     */
    public function roomFrameAdd(Request $request)
    {
        $frameName = $request->input('frame_name', '');
        $frameImg = $request->input('frame_img', '');
        $frameDesc = $request->input('frame_desc', '');
        if(empty($frameName) || empty($frameImg)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('ROOMFRAME:roomFrameAdd', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->roomFrameService->roomFrameAdd($frameName, $frameImg, $frameDesc);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 房间边框编辑
     */
    public function roomFrameEdit(Request $request)
    {
        $frameId = $request->input('frame_id', 0);
        $frameName = $request->input('frame_name', '');
        $frameImg = $request->input('frame_img', '');
        $frameDesc = $request->input('frame_desc', '');
        if(empty($frameId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('ROOMFRAME:roomFrameEdit', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->roomFrameService->roomFrameEdit($frameId, $frameName, $frameImg, $frameDesc);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 房间边框用户下发记录新增
     */
    public function roomFrameUserAdd(Request $request)
    {
        $frameId = $request->input('frame_id', 0);
        $roomId = $request->input('room_id', 0);
        $timeSt = $request->input('time_st', '');
        $timeEt = $request->input('time_et', '');
        $adminId = (int) $request->input('admin_id', 0);
        if(empty($frameId) || empty($roomId) || empty($timeSt) || empty($timeEt) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $timeSt = date("Y-m-d H:i:s", strtotime($timeSt));
        $timeEt = date("Y-m-d H:i:s", strtotime($timeEt));
        //锁
        $ifLock = redis_lock('ROOMFRAME:roomFrameUserAdd', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->roomFrameService->roomFrameUserAdd($frameId, $roomId, $timeSt, $timeEt, $adminId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 房间边框用户下发记录编辑
     */
    public function roomFrameUserEdit(Request $request)
    {
        $logId = $request->input('log_id', 0);
        $timeSt = $request->input('time_st', '');
        $timeEt = $request->input('time_et', '');
        $adminId = (int) $request->input('admin_id', 0);
        if(empty($logId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $timeSt = empty($timeSt) ? '' : date("Y-m-d H:i:s", strtotime($timeSt));
        $timeEt = empty($timeEt) ? '' : date("Y-m-d H:i:s", strtotime($timeEt));
        if(empty($timeSt) || empty($timeEt)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('ROOMFRAME:roomFrameUserEdit', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->roomFrameService->roomFrameUserEdit($logId, $timeSt, $timeEt, $adminId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 房间边框用户下发记录删除
     */
    public function roomFrameUserDel(Request $request)
    {
        $logId = $request->input('log_id', 0);
        $adminId = (int) $request->input('admin_id', 0);
        if(empty($logId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('ROOMFRAME:roomFrameUserDel', 1);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->roomFrameService->roomFrameUserDel($logId, $adminId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

}